package anc.server.tankshooter.network;

public class Match implements Runnable {

	static enum STATE{
		WAITTING,
		PLAYING
	}
	private STATE mState;
	private Player mPlayer1;
	private boolean mHavePlayer1;
	private Player mPlayer2;
	private boolean mHavePlayer2;
	final private int mIndex;
	private int mMapIndex;
	private int mStartTime;
	private int mEndTime;
	
	public Match(int index){
		this.mIndex = index;
		mState = STATE.WAITTING;
		mPlayer1 = new Player();
		mHavePlayer1 = false;
		mPlayer2 = new Player();
		mHavePlayer2 = false;
		mMapIndex = 1;
		mStartTime = 0;
		mEndTime = 0;
	}

	
	/**
	 * Khi một Client JoinRoom
	 * @param player
	 * @return
	 */
	public boolean JoinPlayer(Player player)
	{
		if(!mHavePlayer1) 
		{ 
			mPlayer1 = player;
			mHavePlayer1 = true;
			mPlayer1.ClientJoinRoomSuccess(getStringValue());
			return true;
		}
		else if (!mHavePlayer2) 
		{ 
			mPlayer2 = player;
			mHavePlayer2 = true;
			mPlayer1.ClientJoinRoomSuccess(getStringValue());
			mPlayer2.ClientJoinRoomSuccess(getStringValue());
			return true;
		}
		else return false;
	}
	
	/**
	 * Khi một Client LeaveRoom
	 * @param player
	 * @return
	 */
	public boolean LeavePlayer(Player player)
	{
		boolean resul = false;
		if (player == mPlayer1)
		{
			resul = true;
			if(mHavePlayer2)
			{
				mPlayer1 = mPlayer2;
				mPlayer2 = new Player();
				mHavePlayer2 = false;
			}else
			{
				mPlayer1 = new Player();
				mHavePlayer1 = false;
			}
		}else if(player == mPlayer2)
		{
			resul = true;
			mPlayer2 = new Player();
			mHavePlayer2 = false;
		}
		return resul;
	}
	
	/**
	 * Khi Host bắt đầu game
	 * @param MapIndex
	 */
	public void StartGame(int MapIndex)
	{
		this.mMapIndex = MapIndex;
		mPlayer1.StartGame(mMapIndex);
		mPlayer2.StartGame(mMapIndex);
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	public void Pause()
	{
		mPlayer1.Pause();
		mPlayer2.Pause();
	}
	public void Resume()
	{
		mPlayer1.Resume();
		mPlayer2.Resume();
	}
	public void Run(String dir, int id)
	{
		mPlayer1.Run(dir, id);
		mPlayer2.Run(dir, id);
	}
	public void Fire(int id, String xy)
	{
		mPlayer1.Fire(id, xy);
		mPlayer2.Fire(id, xy);
	}
	public void ClientGiveUp(int id)
	{
		mPlayer1.ClientGiveUp(id);
		mPlayer2.ClientGiveUp(id);
	}
	public void ClientDisconnect(Player player)
	{
		if(player == mPlayer1)
		{
			if(mHavePlayer2)
			{
				mPlayer2.ClientGiveUp(player.getAccount().getAccountId());
				LeavePlayer(player);
			}
		}else
		if(player == mPlayer2)
		{
			if(mHavePlayer1)
			{
				mPlayer1.ClientGiveUp(player.getAccount().getAccountId());
				LeavePlayer(player);
			}
		}
		LeavePlayer(player);
	}
	public int getPlayer1ID() { return mPlayer1.getAccount().getAccountId(); }
	public String getPlayer1Name() { return mPlayer1.getAccount().getName(); }
	
	public int getPlayer2ID() { return mPlayer2.getAccount().getAccountId(); }
	public String getPlayer2Name() { return mPlayer2.getAccount().getName(); }
	
	public int getMatchState() { return mState.ordinal(); }

	public int getIndex() { return this.mIndex; }
	
	public String getStringValue()
	{
		String value = mIndex + ":" + mPlayer1.getAccount().getAccountId() + ":" + mPlayer1.getAccount().getUsername() + ":" +
							        + mPlayer2.getAccount().getAccountId() + ":" + mPlayer2.getAccount().getUsername() + ":" +
							        + mMapIndex + ":" + mStartTime + ":" + mEndTime;
		return value;
	}
}
